package spring.yongongsystem.controller.admin;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import spring.yongongsystem.POJO.dto.AdminPageQueryDTO;
import spring.yongongsystem.POJO.dto.MerchantDTO;
import spring.yongongsystem.POJO.dto.ProductPageQueryDTO;
import spring.yongongsystem.POJO.entity.Product;
import spring.yongongsystem.result.PageResult;
import spring.yongongsystem.result.Result;
import spring.yongongsystem.service.ProductService;
@CrossOrigin(origins = "http://localhost:5173")
@RestController
@RequestMapping("/admin/product")
@Slf4j
@Api(tags = "管理员产品相关接口")
public class ProductController {

    @Autowired
    private ProductService productService;
    /**
     * 新增产品
     * @param product
     * @return
     */
    @PostMapping
    @ApiOperation("新增产品")
    public Result save(@RequestBody Product product){
        log.info("新增产品：{}",product);
        productService.save(product);
        return Result.success();
    }

    /**
     * 编辑产品信息
     * @param product
     * @return
     */
    @PutMapping
    @ApiOperation("编辑产品信息")
    public Result update(@RequestBody Product product){
        log.info("编辑产品信息：{}", product);
        productService.update(product);
        return Result.success();
    }

    /**
     * 删除产品
     * @param id
     * @return
     */
    @DeleteMapping
    @ApiOperation("删除产品")
    public Result<String> deleteById(Long id){
        log.info("删除产品：{}", id);
        productService.deleteById(id);
        return Result.success();
    }

    /**
     * 产品分页查询
     * @param productPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("产品分页查询")
    public Result<PageResult> page(ProductPageQueryDTO productPageQueryDTO){
        log.info("产品分页查询，参数为：{}", productPageQueryDTO);
        PageResult pageResult = productService.pageQuery(productPageQueryDTO);
        return Result.success(pageResult);
    }
}
